package com.zhourh.bang.modular.edu.controller;

import com.zhourh.bang.common.controller.BaseController;
import com.zhourh.bang.common.exception.BizExceptionEnum;
import com.zhourh.bang.common.exception.BussinessException;
import com.zhourh.bang.common.node.ZTreeNode;
import com.zhourh.bang.common.persistence.dao.SMajorMapper;
import com.zhourh.bang.common.persistence.model.SMajor;
import com.zhourh.bang.core.util.ToolUtil;
import com.zhourh.bang.modular.edu.dao.MajorDao;
import com.zhourh.bang.modular.edu.service.IMajorService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 专业控制器
 *
 * @author fengshuonan
 * @Date 2018-06-22 10:53:47
 */
@Controller
@RequestMapping("/major")
public class MajorController extends BaseController {

    private String PREFIX = "/edu/major/";

    @Resource
    SMajorMapper majorMapper;

    @Resource
    MajorDao majorDao;

    @Resource
    IMajorService majorService;

    /**
     * 跳转到专业首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "major.html";
    }

    /**
     * 跳转到添加专业
     */
    @RequestMapping("/major_add")
    public String majorAdd() {
        return PREFIX + "major_add.html";
    }

    /**
     * 跳转到修改专业
     */
    @RequestMapping("/major_update/{majorId}")
    public String majorUpdate(@PathVariable Integer majorId, Model model) {
        SMajor major = this.majorMapper.selectById(majorId);
        model.addAttribute("major",major);
        return PREFIX + "major_edit.html";
    }

    /**
     * 获取专业的tree列表
     */
    @RequestMapping(value = "/tree")
    @ResponseBody
    public List<ZTreeNode> tree() {
        List<ZTreeNode> tree = this.majorDao.tree();
        tree.add(ZTreeNode.createParent());
        return tree;
    }

    /**
     * 获取专业列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String key) {
        return this.majorDao.list(key);
    }

    /**
     * 新增专业
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(SMajor major) {
        if(ToolUtil.isOneEmpty(major,major.getName(),major.getCode())){
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        major.insert();
        return super.SUCCESS_TIP;
    }

    /**
     * 删除专业
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(Integer majorId) {
        return this.majorMapper.deleteById(majorId);
    }


    /**
     * 修改专业
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(SMajor major) {
        if(ToolUtil.isOneEmpty(major,major.getName(),major.getCode())){
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        majorMapper.updateById(major);
        return super.SUCCESS_TIP;
    }

    /**
     * 专业详情
     */
    @RequestMapping(value = "/detail")
    @ResponseBody
    public Object detail(Integer majorId) {
        return this.majorMapper.selectById(majorId);
    }
}
